<?php
namespace app\sns\controller;

use data\extend\Aes;
use data\extend\ErrCode;
use data\extend\H;
use data\extend\Send;
use data\service\UserService;
use think\Exception;
use think\facade\Request;
use think\facade\Cache;

/**
 * API鉴权验证
 */
class Oauth
{
    use Send;

    /**
     * 认证授权 通过用户信息和路由
     * @param Request $request
     * @return \Exception|UnauthorizedException|mixed|Exception
     * @throws UnauthorizedException
     */
    final function authenticate()
    {
        return self::getClient();
    }
    final function loginauth()
    {
        return self::getLogin();
    }

    public static function getLogin(){
        //获取头部信息
        try {
            $authorization = Request::header('token');   //获取请求中的authorization字段
            if($authorization == ""){
                return self::returnMsg(ErrCode::$UserNoLogin,'user no login',[]);
            }
            $uid = Aes::aesDe($authorization);
            //查询用户是否存在
            $userService = new UserService();
            $userInfo = $userService->getUserInfoByUid($uid) ;
            if(empty($userInfo)){
                return self::returnMsg(ErrCode::$UserNoLogin,'user ck no login',[]);
            } else{
                return $userInfo;
            }
        } catch (Exception $e) {
            return self::returnMsg(ErrCode::$IllegalAuthorization,'Invalid authorization credentials',[]);
        }
    }

    /**
     * 参数校验
     * @param Request $request
     * @return $this
     * @throws UnauthorizedException
     */
    public static function getClient()
    {
        //获取头部信息
        try {
            $post = request()->post('');
            ksort($post);

            $authorization = Request::header('authorization');   //获取请求中的authorization字段
            $authorizationInfo  = base64_decode($authorization);  //对base_64解密

            //获取当前时间戳
            $microTime =  getMillisecond() - $post['_t'];
            //接口10分钟有效
            if($microTime >= 600000){
                return self::returnMsg(ErrCode::$IllegalInterface,'The interface has expired',[]);
            }
            $queryStr = "";
            foreach($post as $k => $v){$queryStr .= $k .'=' .$v .'&';}

            $sign = Aes::aesEn(rtrim($queryStr, "&"));

            if($sign == $authorizationInfo){
                return true;
            } else {
                return self::returnMsg(ErrCode::$IllegalAuthorization,'Invalid authorization credentials',[]);
            }
        } catch (Exception $e) {
            return self::returnMsg(ErrCode::$IllegalAuthorization,'Invalid authorization credentials',[]);
        }
    }


    /**
     * 检测当前控制器和方法是否匹配传递的数组
     *
     * @param array $arr 需要验证权限的数组
     * @return boolean
     */
    public static function match($arr = [])
    {
        $request = Request::instance();
        $arr = is_array($arr) ? $arr : explode(',', $arr);
        if (!$arr)
        {
            return false;
        }
        $arr = array_map('strtolower', $arr);

        // 是否存在
        if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr))
        {
            return true;
        }

        // 没找到匹配
        return false;
    }
}
